/**************************************************************************/
/*	名称:CY8CMBR3XXX   													  */
/*	作用:触摸芯片初始化													  */
/*	作者：yuleying 														  */
/*	创建日期：20150516													  */
/*	修改：																  */
/*																		  */
/**************************************************************************/
//------------------
#include "userConfig.h"
#include "CY8CMBR3xxx.h"
#include "key.h"
/* OS 相关头文件 */
#include "osInclude.h"

//--CY8CMBR3116配置参数，使用EZ-Click 2.0自动生成----------
//--电容阀值300，支持接近感应，屏蔽相临通道，使能自动阀值,关闭中值滤波,关闭保护电极,用软件检测BIT2为1时发生了水流
const uint8_t ucCY8CMBR3116Configuration[128] = {
    0xEEu, 0x7Fu, 0xEAu, 0x7Fu, 0x00u, 0x00u, 0x00u, 0x00u,
    0xB8u, 0xA8u, 0xAAu, 0x2Au, 0x7Fu, 0x80u, 0x80u, 0x80u,
    0x7Fu, 0x80u, 0x80u, 0x80u, 0x80u, 0x80u, 0x80u, 0x80u,
    0x80u, 0x80u, 0x80u, 0x7Fu, 0x03u, 0x00u, 0x00u, 0x00u,
    0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x80u,
    0x05u, 0x00u, 0x00u, 0x02u, 0x00u, 0x02u, 0x00u, 0x00u,
    0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x1Eu, 0x00u,
    0x00u, 0x00u, 0x1Eu, 0x00u, 0x00u, 0x00u, 0x01u, 0x01u,
    0x01u, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu,
    0xFFu, 0x00u, 0x00u, 0x00u, 0x24u, 0x02u, 0x01u, 0x59u,
    0x00u, 0x37u, 0x01u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
    0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
    0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
    0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
    0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
    0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x09u, 0x4Fu
};


//读寄存器
__STATIC_INLINE uint8_t ucRdReg(uint8_t ucRegisterAddr, uint8_t *pucReadBuf, uint16_t usDataLen)
{
	return ucI2cMasterReadRegister(I2C0, TOUCH_DEVICE_ADDR, ucRegisterAddr, pucReadBuf, usDataLen);
}

//写寄存器
__STATIC_INLINE uint8_t ucWtReg(uint8_t ucRegisterAddr, uint8_t *pxSendBuf, uint16_t usDataLen)
{
	return ucI2cMasterWriteRegister(I2C0, TOUCH_DEVICE_ADDR, ucRegisterAddr, pxSendBuf, usDataLen);
}

//初始化
bool bCy8cmbr3xxxInit(void)
{
 	uint8_t ucRegBuf[2];
	uint8_t i,j;

	i = 0;
	while(1)
	{
		if(ucRdReg(REG_CONFIG_CRC,ucRegBuf,LEN_CONFIG_CRC) == FAIL)			//读CRC，判断是否已经初始化
		{
			TOUCH_PRINTF("[CYx3xx]ReadCRCErr\n");
			if(++i >= 10)
			{
				return false;
			}
			continue;		
		}
		break;
	}	
	TOUCH_PRINTF("[CYx3xx]CRC=%02x,%02x\n",ucRegBuf[0],ucRegBuf[1]);	
 	if(memcmp(ucCY8CMBR3116Configuration+(sizeof(ucCY8CMBR3116Configuration)-LEN_CONFIG_CRC), \
		ucRegBuf,LEN_CONFIG_CRC) == 0)		//已经被初始化
 	{
		TOUCH_PRINTF("[CYx3xx]CrcCheckOk\n");	
 		return true;		//成功	
 	}
 	else			//未被初始化
 	{	
		TOUCH_PRINTF("[CYx3xx]init start\n");		
		i = 3;
		while(i--)
		{
			//禁用传感器 37 W 00 00 00 
			ucRegBuf[0] = (CMD_DISABLE_SENSOR >> 8) & 0xff;
			ucRegBuf[1] = CMD_DISABLE_SENSOR & 0xff;
			if(ucWtReg( REG_SENSOR_EN, ucRegBuf, 2) == FAIL)//写寄存器值
			{
				TOUCH_PRINTF("[CYx3xx]Wrt SENSOR_EN Err\n");				
				continue;		//错误
			}
			
			//下发配置 37 W 00 EF 7F ......					
			if(ucWtReg(REG_SENSOR_EN, (uint8_t *)ucCY8CMBR3116Configuration, sizeof(ucCY8CMBR3116Configuration)) == FAIL)//写寄存器值
			{
				TOUCH_PRINTF("[CYx3xx]WrtAllConfigErr\n");				
				continue;		//错误
			}				
		
			//读控制寄存器,检查命令是否操作完成,37 W 86 S / 37 R xx
			j = 5;
			do{
				if(ucRdReg(REG_CTRL_CMD, ucRegBuf, LEN_CTRL_CMD) == FAIL)//读寄存器值
				{
					TOUCH_PRINTF("[CYx3xx]ReadCTRLRegErr\n");
					continue;		//错误
				}				
				if(ucRegBuf[0] == CMD_NOT_OP)
				{
					break;	//跳出do_while
				}
			}while(--j);

			//保存配置 37 W 86 02
			ucRegBuf[0] = CMD_SAVE_CONFIG;
			if(ucWtReg(REG_CTRL_CMD, ucRegBuf, LEN_CTRL_CMD) == FAIL)//将配置写入flash
			{
				TOUCH_PRINTF("[CYx3xx]Wrt SAVE_CONFIG Err\n");			
				continue;		//错误
			}	
			
			//检查命令是否操作完成37 W 86 S / 37 R xx
			j = 50;	//时间适当留长些
			do{
				bOsDelayMs(50);	//等待操作命令结束
				if(ucRdReg(REG_CTRL_CMD, ucRegBuf, LEN_CTRL_CMD) == FAIL)		//读寄存器值
				{
					TOUCH_PRINTF("[CYx3xx]ucRd CTRL_CMD Reg Err\n");
					continue;		//错误
				}				
				if(ucRegBuf[0] == CMD_NOT_OP)	//操作结束
				{
					break;	//跳出do_while
				}			
			}while(--j);
			
		
			//检查操作命令是否有错误 37 W 88 S / 37 R xx		
			if(ucRdReg(REG_CTRL_CMD_STATUS, ucRegBuf, LEN_CTRL_CMD_STATUS) == FAIL)			//读CTRL_CMD_STATUS，
			{
				TOUCH_PRINTF("[CYx3xx]REG_CTRL_CMD_STATUS Err\n");
				continue;		//错误
			}				
			if(ucRegBuf[0] != CMD_NOT_ERR)
			{
				TOUCH_PRINTF("[CYx3xx]REG_CTRL_CMD_STATUS = %02x\n",ucRegBuf[0]);
				continue;	//有错误重新初始化
			}

			
			//检查操作命令是否有错误 37 W 89 S / 37 R xx		
			if(ucRdReg(REG_CTRL_CMD_ERR, ucRegBuf, LEN_CTRL_CMD_ERR) == FAIL)			//读CTRL_CMD_STATUS，
			{
				TOUCH_PRINTF("[CYx3xx]Read CMD_STATUS Reg Err\n");
				continue;		//错误
			}				
			if(ucRegBuf[0] != CMD_CTRL_CMD_SUCCESS)
			{
				TOUCH_PRINTF("[CYx3xx]REG_CTRL_CMD_ERR = %02x\n",ucRegBuf[0]);
				continue;	//有错误重新初始化
			}
						
			//重启芯片
			ucRegBuf[0] = CMD_RESET_CHIP;
			j = 3;	
			do{
				if(ucWtReg(REG_CTRL_CMD, ucRegBuf, LEN_CTRL_CMD) == SUCCESS)//写寄存器值
				{
					TOUCH_PRINTF("[CYx3xx]init success\n");	
					return true;
				}
				TOUCH_PRINTF("[CYx3xx]CMD_RESET_CHIP Err\n");				
			}while(--j);	

			TOUCH_PRINTF("[CYx3xx]init ERR\n");					
			return false;
		}
		TOUCH_PRINTF("[CYx3xx]init ERR\n");		
		return false;
 	}
}



uint8_t ucReadKeyValue(uint8_t *pucBuf)
{
	return ucRdReg(REG_BUTTON_STATE,pucBuf,BUTTON_STATE_LEN);
}






